package com.kong.controller;

import com.kong.consts.TableBeanEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.burgeon.commons.unique.IDWorker;
import org.seed.mybatis.core.PageInfo;
import org.seed.mybatis.core.annotation.DbTypeEnum;
import org.seed.mybatis.core.query.Query;
import org.seed.mybatis.core.query.param.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.burgeon.commons.react.Result;
import com.kong.dao.entity.JavaTemplate;
import com.kong.service.JavaTemplateService;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * Java文件模版
 *
 * @author glin
 * @date 2024-04-16
 */
@RestController
@RequestMapping("java-template")
@Tag(name = "Java文件模版")
public class JavaTemplateController {

    @Autowired
    private JavaTemplateService javaTemplateService;

    /**
     * 分页查询
     *
     * @param param 分页参数
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    @Parameters({
            @Parameter(name = "pageIndex", description = "当前页码"),
            @Parameter(name = "pageSize", description = "每页记录数")
    })
    public Result<PageInfo<JavaTemplate>> page(PageParam param) {
        Query query = param.toQuery();
        PageInfo<JavaTemplate> pageInfo = javaTemplateService.page(query);
        return Result.ok(pageInfo);
    }

    /**
     * 新增记录
     */
    @PostMapping("/save")
    @Operation(summary = "新增记录")
    public Result<String> save(String name,
                               String relativePath,
                               String fileName,
                               String content,
                               String groupBelong) {
        if (!TableBeanEnum.supportType().contains(name)) {
            String all = TableBeanEnum.supportType().stream().collect(Collectors.joining(","));
            return Result.errorMessageOnly("name仅支持如下数据类型: " + all);
        }
        JavaTemplate javaTemplate = new JavaTemplate();
        javaTemplate.setId(IDWorker.nextId());
        javaTemplate.setName(name);
        javaTemplate.setRelativePath(relativePath);
        javaTemplate.setFileName(fileName);
        javaTemplate.setContent(content);
        javaTemplate.setGroupBelong(groupBelong);
        javaTemplate.setUpdatedAt(new Date());
        javaTemplateService.saveIgnoreNull(javaTemplate);
        // 返回添加后的主键值
        return Result.ok(javaTemplate.getId());
    }

    /**
     * 修改记录
     *
     * @param javaTemplate 表单数据
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "修改记录")
    public Result<Void> update(
            String id,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String relativePath,
            @RequestParam(required = false) String fileName,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) String groupBelong) {
        JavaTemplate store = javaTemplateService.getById(id);
        if (store == null) {
            return Result.errorMessageOnly("无数据记录");
        }
        if (StringUtils.hasText(name) && !TableBeanEnum.supportType().contains(name)) {
            String all = TableBeanEnum.supportType().stream().collect(Collectors.joining(","));
            return Result.errorMessageOnly("name仅支持如下数据类型: " + all);
        }
        store.setName(name);
        store.setRelativePath(relativePath);
        store.setFileName(fileName);
        store.setContent(content);
        store.setGroupBelong(groupBelong);
        javaTemplateService.updateIgnoreNull(store);
        return Result.ok();
    }

    /**
     * 删除记录
     *
     * @param id 主键id
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除记录")
    public Result<Void> delete(String id) {
        javaTemplateService.deleteById(id);
        return Result.ok();
    }


    /**
     * 根据ID查询
     *
     * @param id 主键id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "根据ID查询")
    public Result<JavaTemplate> get(String id) {
        JavaTemplate javaTemplate = javaTemplateService.getById(id);
        return Result.ok(javaTemplate);
    }

}